#!/bin/bash
########################################################################
# script name:  update_code_by_tag.ssh
# Used for:
# Author:       Martin Dumont
# Contributors:
# Version:      0.1
#
# (date format: date +'%Y/%m/%d')
#
# Date          Version Contributors    Modifications
# ----------    ------- ------------    -------------
# 2011/12/16    0.1     MDUMONT         initial
########################################################################
usage()
{
cat << STOP

usage: $0 [options] [ARG]

This script is used for ....

OPTIONS:
   -h               Hosts [all]
   -t               Tag (see tags with "git tag -l" or specify "latest")
   -r               Repository (sap|dispatch|files|web)

Ex.:
$0 -h sapbcp01 -t v1.0.4 -r web
$0 -h qa -t v1.0.5 -r sap
$0 -h prd -t latest -r files
$0 -h saphrt01,saphrt02 -t v1.0.5 -r files
STOP
}
#######################################
# Get parameters
#######################################
while getopts h:t:r: OPTION
do
        case $OPTION in
        h)
                HOSTS=$(/bin/echo $OPTARG | tr '[A-Z]' '[a-z]' | sed 's/,/ /g')
                ;;
        t)
                TAG=$(/bin/echo $OPTARG)
                ;;
        r)
                REPO=$(/bin/echo $OPTARG)
                ;;
        \?)
                usage
                exit 1
                ;;
        *)
                usage
                exit 1
                ;;
        esac
done
#if [ $OPTIND = 1 ]; then usage; exit 1; fi
if [ -z "$HOSTS" ]; then HOSTS=localhost; fi
if [ -z "$TAG" ]; then TAG=latest; fi
if [ -z "$REPO" ]; then REPO=sap; fi
#######################################
# Variables and environnement
#######################################
BAENV=$(dirname `which $0`)/loadenv.sh
if [ -f $BAENV ]; then . $BAENV; else echo "No env library ($BAENV)"; exit 1; fi
GET_TAR=false
MAINDIR=/var/code
DIR=$MAINDIR/${REPO}.$RANDOM
DEPOT=/central/code/depot
VERSIONFILE=$DEPOT/${REPO}.latest_version.txt
ORIGIN=$ORIGIN/$REPO/
LATESTCOMMIT=$(git ls-remote $ORIGIN HEAD | cut -f1)
VERSION=$(cat $VERSIONFILE)
LATEST=$(echo ${REPO}.git_${VERSION}.tar.gz)
SSH_OPTS=" -x -q -o ConnectTimeout=15 -o BatchMode=yes -o StrictHostKeyChecking=no -o LogLevel=fatal"
NEWHOSTLIST=$(checkhost $HOSTS | grep -v Verifying)
if [ "$TAG" = "latest" ]
then
	GET_TAR=true
	if [ ! -f $VERSIONFILE ]
	then
		echo "Missing version file $VERSIONFILE"
		exit 1
	fi
fi
if [ "$GET_TAR" = "false" ]; then 
	if /usr/bin/git ls-remote --tags git://saprmt/${REPO} $TAG | /bin/grep -q $TAG
	then
		VERSION=$TAG
	else
		/bin/echo Tag $TAG not found
		exit 1
	fi
else
	ORIGIN=sapnfs:/nfs/code/depot
	CKSUM=/tmp/code_cksum.$RANDOM
	(cd $DEPOT/; umask 022 && sudo -u git cksum $LATEST > $CKSUM)
	diff $CKSUM $DEPOT/$LATEST.cksum 2>&1 > /dev/null
	RC=$?
	rm $CKSUM 2>&1 > /dev/null
	if [ "$RC" != "0" ]; then echo "CKSUM of $DEPOT/$LATEST doesn't match $DEPOT/$LATEST.cksum"; exit 1; fi
fi
#######################################
# MAIN
#######################################
/bin/echo Extracting $VERSION tag from remote origin $ORIGIN to $DIR
for HOSTNAME in $NEWHOSTLIST
do
	cat <<-EOF | if [ "$HOSTNAME" != "localhost" ]; then sudo -u saunix ssh $SSH_OPTS $HOSTNAME "cat | sudo bash"; else cd /tmp; cat | sudo bash; fi
		umask 022
		if [ ! -d $MAINDIR ]; then mkdir -m 755 -p $MAINDIR; else chmod 755 $MAINDIR; fi
		mkdir -m 755 -p $DIR
		cd $DIR
		if [ "$GET_TAR" = "false" ]; then
			/usr/bin/git archive --format=tar.gz --remote=$ORIGIN $VERSION | gtar -zx --mode=755 -f - && /bin/echo $VERSION > .version
		else
			gtar -zx --mode=755 -f $DEPOT/$LATEST && echo $VERSION > .version
		fi
		cd $MAINDIR
		if [ -d $MAINDIR/${REPO} ]; then \rm -rf $MAINDIR/${REPO}; fi
		ln -fs $DIR ${REPO}
		ls -1td $MAINDIR/* | grep $REPO | tail -n +4 | xargs \rm -rf
		if [ -f $DIR/.version ]
		then
			/bin/echo "$HOSTNAME: Version $VERSION has been pushed to $MAINDIR/${REPO} (linked to $DIR)"
		else
			/bin/echo "$HOSTNAME: Something went wrong, I don't see any $DIR/.version file"
		fi
	EOF
done
#######################################
exit 0
